package com.home.webapp.base.service.impl;

import com.home.base.core.framework.msg.PageResponse;
import com.home.base.core.framework.msg.ServerResponse;
import com.home.webapp.common.constants.dict.DictDataStatusConstant;
import com.home.common.constants.redis.RedisDateBase;
import com.home.common.framework.core.entity.EntityUtils;
import com.home.common.framework.core.service.impl.BaseServiceImpl;
import com.home.common.handler.exception.BizException;
import com.home.common.redis.RedisUtil;
import com.home.common.utils.query.QueryParams;
import com.home.webapp.base.entity.BaseDictData;
import com.home.webapp.base.mapper.BaseDictDataMapper;
import com.home.webapp.base.service.BaseDictDataService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.home.webapp.common.entity.DictDataOut;
import com.home.webapp.common.entity.DictTypeOut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Description: 字典数据表 serviceImpl
 * Created by 王大宸 on 2020-10-05 23:28:38
 * Created with IntelliJ IDEA.
 */
@Service
public class BaseDictDataServiceImpl extends BaseServiceImpl<BaseDictDataMapper, BaseDictData, String> implements BaseDictDataService {
    private static final Logger logger = LoggerFactory.getLogger(BaseDictDataServiceImpl.class);

    /***
     * 功能说明：分页
     *
     * @author 王大宸
     * @date 2020/10/15 0:23
     * @param params
     * @return com.home.common.framework.msg.PageResponse
     */
    @Override
    public PageResponse<BaseDictData> page(Map<String, Object> params) {
        QueryParams queryParams = new QueryParams(params);
        Page<BaseDictData> page = PageHelper.startPage(queryParams.getPage(), queryParams.getLimit());
        List<BaseDictData> list = baseMapper.page(queryParams);
        return PageResponse.ok(page.getTotal(), list);
    }

    /***
     * 功能说明：新增
     *
     * @author 王大宸
     * @date 2020/8/19 15:51
     * @param entity 数据字典
     * @return com.home.common.framework.msg.ServerResponse<com.home.web.base.entity.BaseDictData>
     */
    @Override
    public BaseDictData saveInfo(BaseDictData entity) throws BizException {
        BaseDictData dictLabel = baseMapper.getByTypeAndValue(entity.getDictType(), entity.getDictValue());
        if (null != dictLabel) {
            throw new BizException("值【" + entity.getDictValue() + "】已经存在!");
        }
        EntityUtils.toCreatAndUpdateEntity(entity);
        baseMapper.insertSelective(entity);
        updateRedis(entity);
        return entity;
    }

    /***
     * 功能说明：更新
     *
     * @author 王大宸
     * @date 2020/8/19 15:51
     * @param entity 数据字典
     * @return com.home.common.framework.msg.ServerResponse<com.home.web.base.entity.BaseDictData>
     */
    @Override
    public BaseDictData updateInfo(BaseDictData entity) throws BizException {
        BaseDictData one = baseMapper.getByTypeAndValue(entity.getDictType(), entity.getDictValue());
        if (null != one && !one.getId().equals(entity.getId())) {
            throw new BizException("值【" + entity.getDictValue() + "】已经存在!");
        }
        EntityUtils.toUpdateEntity(entity);
        baseMapper.updateByPrimaryKeySelective(entity);
        updateRedis(entity);
        return entity;
    }

    /**
     * 删除数据字典
     *
     * @param id 需要删除数据的id
     * @return
     * @throws BizException
     */
    @Override
    public int remove(String id) throws BizException {
        BaseDictData baseDictData = baseMapper.selectByPrimaryKey(id);
        baseDictData.setStatus("0");
        baseMapper.updateByPrimaryKeySelective(baseDictData);
        updateRedis(baseDictData);
        return 1;
    }

    /***
     * 功能说明：更新状态
     *
     * @author 王大宸
     * @date 2020/10/30 13:19
     * @param dictData dictData
     * @return com.home.common.framework.msg.ServerResponse<com.home.web.base.entity.BaseDictData>
     */
    @Override
    public ServerResponse<BaseDictData> updateStatus(BaseDictData dictData) {
        baseMapper.updateByPrimaryKeySelective(dictData);
        BaseDictData baseDictData = baseMapper.selectByPrimaryKey(dictData.getId());
        updateRedis(baseDictData);
        return ServerResponse.ok();
    }

    /***
     * 功能说明：更新 redis
     *
     * @author 王大宸
     * @date 2020/10/30 11:59
     * @param entity entity
     * @return void
     */
    private void updateRedis(BaseDictData entity) {
        // 添加成功之后更新 redis
        DictTypeOut dictType = baseMapper.getByDictType(entity.getDictType());
        boolean hasKey = RedisUtil.hasKey(RedisDateBase.DICT_DATA_REDIS_KEY + dictType.getDictType(), RedisDateBase.DEFAULT_DATE_BASE);
        if (!hasKey) {
            // 初始化数据字典类型信息
            RedisUtil.set(RedisDateBase.DICT_DATA_REDIS_KEY + dictType.getDictType(), dictType, RedisDateBase.DEFAULT_DATE_BASE);
        }
        List<DictDataOut> list = baseMapper.getType(dictType.getDictType(), DictDataStatusConstant.IS_OK);
        // 更新集合
        RedisUtil.set(RedisDateBase.DICT_DATA_REDIS_KEY + dictType.getDictType(), dictType, RedisDateBase.DEFAULT_DATE_BASE);
        // 添加单个到redis
        for (DictDataOut data : list) {
            if (data.getDictValue().equals(entity.getDictValue())) {
                RedisUtil.set(RedisDateBase.DICT_DATA_REDIS_KEY + data.getDictType() + "_" + data.getDictValue(), data, RedisDateBase.DEFAULT_DATE_BASE);
                break;
            }
        }
        // 查询被禁用的数据字典
        List<DictDataOut> lockeList = baseMapper.getType(dictType.getDictType(), DictDataStatusConstant.LOCKED_DICT_DATA);
        if (null != lockeList && 0 < lockeList.size()) {
            for (DictDataOut data : lockeList) {
                RedisUtil.set(RedisDateBase.DICT_DATA_REDIS_KEY + data.getDictType() + "_" + data.getDictValue(), data, RedisDateBase.DEFAULT_DATE_BASE);
            }
        }


//        // 添加成功之后更新 redis
//        SysDictType dictType = sysDictDataMapper.getByDictType(entity.getDictType());
//        boolean hasKey = RedisUtil.hasKey(RedisDateBase.DICT_DATA_REDIS_KEY + dictType.getDictType(), RedisDateBase.DEFAULT_DATE_BASE);
//        if (!hasKey) {
//            // 初始化数据字典类型信息
//            RedisUtil.set(RedisDateBase.DICT_DATA_REDIS_KEY + dictType.getDictType(), dictType, RedisDateBase.DEFAULT_DATE_BASE);
//        }
//        List<SysDictData> list = sysDictDataMapper.getType(RedisDateBase.DICT_DATA_REDIS_KEY + dictType.getDictType(), DictDataStatusConstant.IS_OK);
//        // 更新集合
//        RedisUtil.set(RedisDateBase.DICT_DATA_REDIS_KEY + dictType.getDictType() + RedisDateBase.CHILDREN_SUFFIX, list, RedisDateBase.DEFAULT_DATE_BASE);
//        // 添加单个到redis
//        for (SysDictData sysDictData : list) {
//            if (sysDictData.getDictValue().equals(entity.getDictValue())) {
//                RedisUtil.set(RedisDateBase.DICT_DATA_REDIS_KEY + dictType.getDictType() + "_" + sysDictData.getDictValue(), sysDictData, RedisDateBase.DEFAULT_DATE_BASE);
//                break;
//            }
//        }
//        // 查询被禁用的数据字典
//        List<SysDictData> lockeList = sysDictDataMapper.getType(RedisDateBase.DICT_DATA_REDIS_KEY + dictType.getDictType(), DictDataStatusConstant.LOCKED_DICT_DATA);
//        if (null != lockeList && 0 < lockeList.size()) {
//            for (SysDictData sysDictData : lockeList) {
//                RedisUtil.set(RedisDateBase.DICT_DATA_REDIS_KEY + dictType.getDictType() + "_" + sysDictData.getDictValue(), sysDictData, RedisDateBase.DEFAULT_DATE_BASE);
//            }
//        }
    }

}
